<?php
/*
 * datagrid of projects displayed asynchronoulsly in listprojects.php
*/
include("../inc_files/utils/checksession.php");
// Create connection
	include("../inc_files/utils/dbconnection.php");
	//query for all projects not deleted
	$query ="SELECT staff.FirstName, staff.LastName,	projects.ProjectID,
 projects.ProjectTitle, customers.Company		
			 FROM projects LEFT JOIN staff ON projects.ProjectManager = staff.UserID
LEFT JOIN customers ON customers.CustomerID = projects.CustomerID
			 WHERE projects.Deleted != 1 ORDER BY projects.ProjectID";
	$result = $mysqli->query( $query );

	//count the number of returned results
	$resultCount = $result->num_rows;

	//if there are some results returned create a table showing the results
	if( $resultCount > 0){
		$rownumber = 0;

		echo "<table class='datagrid'>
			<tr class=\"datagrid headerrow\">

			<th style=\"width:15%\"><a href='#'>ProjectID</a></th>
			<th style=\"width:35%\"><a href='#'>Project Title</a></th>
			<th style=\"width:20%\"><a href='#'>Project Manager</a></th>
			<th style=\"width:20%\"><a href='#'>Customer</a></th>
			<th style=\"width:5%\">Edit</th>
			<th style=\"width:5%\">Delete</th>
			</tr>";

		while($row = mysqli_fetch_array($result))
		{

			if($rownumber %2 == 0)
			{
				echo "<tr class=\"datagrid evenrow\">";
			}
			else
			{
				echo "<tr class=\"datagrid oddrow\">";
			}
			$fullName = $row['FirstName'] ." ". $row['LastName'];
			echo "<td style=\"width:25%\">" . $row['ProjectID'] . "</td>";
			echo "<td style=\"width:40%\">" . $row['ProjectTitle'] . "</td>";
			echo "<td style=\"width:20%\">" . $fullName . "</td>";
			echo "<td style=\"width:10%\">" . $row['Company'] . "</td>";
			echo "<td style=\"width:5%\"><input type=\"image\"  height=\"32\" width=\"32\"  src=\"../images/pencil_32.png\" alt=\"Edit project ". $row['ProjectID'] ."\" onclick=\"window.location.href='editproject.php?projectid="  . $row['ProjectID'] . "'\"></td>";
			echo "<td style=\"width:5%\"><img height=\"32\" width=\"32\"  src=\"../images/edit-delete-2-32.png\" alt=\"Delete records for ". $row['ProjectID'] ."\" onclick=\"deleteproject('"  . $row['ProjectID'] . "')\" style=\"cursor:pointer\"></td>";
			echo "</tr>";
			$rownumber++;
		}
		echo "<tr class=\"datagrid footer\">
					<td colspan=\"4\" style=\"width:80%\"><a href=\"/f21na/projects/newproject.php\">Add new project
					<img src=\"../images/add_32.png\" style=\"border:none\" height=\"32\" width=\"32\" alt=\" Add new project.\" ></a>
					</td>
					<td colspan=\"2\" style=\"width:20%\"></td>
					</tr>";
		echo "</table>";
	}
	//if no results returned say so.
	else
	{

		echo"No records returned";
	}
	//tidy up database connection
	$mysqli->close();


	?>